/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
$(function() {

    $('#edad').hide();
//    $('#alimentos_menu-grid').hide();
//    $('#btn-calcular').hide();
    $('#buscarAlimento').hide();
    $('#buscarCategoria').hide();
    $('#buscarSubcategoria').hide();

    $('#CartaAlimentos_tipomenu').on('change', function() {
        if ($(this).val != '') {
            $('#edad').show();
            $('#CartaAlimentos_edad').on('change', function() {
                if ($(this).val != '') {
                    $('#busqueda').show();

                }
            });

        }
        else {
//           TODO generar alertas de selecion para validar menu  
        }
    });


    $('#btnIngresas').on('click', function(e) {
        e.preventDefault();
        ingresarAlimentos();

    });

    $('#btn-calcular').on('click', function() {
//            TODO calcular val nutricionales para porcion personal
    });

});

//function busquedaValoresNutri(id) {
//    alert(id);
//    $(this).load('../../nutricion/alimentos/buscarValNutricional/id/' + id,
//    function(data) {
//         console.log(data);
//    }).done(function(data)
//    {
//        alert(data);
////    $('#valoresNutricionales').load(baseUrl+"valoresNutricionales/view/id"+data);
//    });
//}

function busquedaValoresNutri(id) {
    alert(id);
    url = '/index.php/../../nutricion/alimentos/buscarValNutricional';
    $.ajax({
        type: "GET", //GET or POST or PUT or DELETE verb
//        url: 'listaAreas', // Location of the service antes
        url: baseUrl + url,
        data: {idAlimento: id}, //Data sent to server
        //contentType: "",  // content type sent to server
        dataType: "json", //Expected data format from server
        //dataType: "text",  //Expected data format from server

        //processdata: true,  //True or False
        success: function(info) {//On Successful service call
            tableBody = '';
            $('#cuerpoTable').children('tr').remove();
            $.each(info, function(key, value) {
                tableBody += '<tr> <td>' + key + '</td> <td>' + value + '</td></tr>';
            });
            $('#cuerpoTable').append(tableBody);

        },
        error: function(info) {//On Successful service call
            alert("en mantenimiento");
        }

//                    error: ServiceFailed // When Service call fails
    });
}


function ingresarAlimentos() {
//    selected te recupera todos los id seleccionados con check no nitas un id
     selected = $("#alimentos_menu-grid").selGridView("getAllSelection");
//     long = select.length;
//     selected = select.substr(1,long);
    acumulador="";
    $.each(selected, function(key, value) {
//                tableBody += '<tr> <td>' + key + '</td> <td>' + value + '</td></tr>';
//                alert(value);
                acumulador+=","+value;
            });
            
    alert(acumulador);
    if (selected != '') {
//     lista=   $(variable selected); algo asi
        url = '/index.php/../../nutricion/alimentos/ingresarAlimentos';
        $.ajax({
            type: "GET", //GET or POST or PUT or DELETE verb
//        url: 'listaAreas', // Location of the service antes
            url: baseUrl + url,
            data: {lista: acumulador}, //Data sent to server cambiale e nombre
            //contentType: "",  // content type sent to server
            dataType: "json", //Expected data format from server
            //dataType: "text",  //Expected data format from server

            //processdata: true,  //True or False
            success: function(info) {
                 alert("ok");
                 alert(info[0]['descripcion']);
//                 todo  llenar tabla como dice el ceviche
//            tableBody = '';
//            $('#cuerpoTable').children('tr').remove();
//            $.each(info, function(key, value) {
//                tableBody += '<tr> <td>' + key + '</td> <td>' + value + '</td></tr>';
//            });
//            $('#cuerpoTable').append(tableBody);

            },
            error: function(info) {//On Successful service call
                alert("en mantenimiento");
            }

//                    error: ServiceFailed // When Service call fails
        });

    }
}